import { useState } from 'react'
interface UseExportOptions<T = any> {
  // 成功后的回调
  onSuccess?: (response: T) => void
}
interface UseExportResult<T = any> {
  // 提交loading状态
  confirmLoading: boolean
  // 处理提交请求的方法
  handleSubmit: (apiRequest: () => Promise<any>) => void
}
const useExport = <T = any,>(options?: UseExportOptions<T>): UseExportResult<T> => {
  const [confirmLoading, setConfirmLoading] = useState(false)
  const { onSuccess } = options || {}
  const handleSubmit = async (apiRequest: () => Promise<any>) => {
    try {
      setConfirmLoading(true)
      //延迟执行
      const response = await apiRequest()
      if (response?.code === 200) {
        onSuccess?.(response)
      }
    } catch (error) {
      console.error('useExport failed:', error)
    } finally {
      setConfirmLoading(false)
    }
  }

  return {
    confirmLoading,
    handleSubmit,
  }
}

export default useExport
